import { PlusOutlined } from "@ant-design/icons";
import {
  type ActionType,
  ModalForm,
  ProFormText,
  ProFormTextArea,
} from "@ant-design/pro-components";
import { useRequest } from "@umijs/max";
import { Button, message } from "antd";
import type { FC } from "react";

interface CreateFormProps {
  reload?: ActionType["reload"];
}

const CreateForm: FC<CreateFormProps> = (props) => {
  const { reload } = props;

  const [messageApi, contextHolder] = message.useMessage();

  const { run, loading } = useRequest(async () => {}, {
    manual: true,
    onSuccess: () => {
      messageApi.success("添加成功");
      reload?.();
    },
    onError: () => {
      messageApi.error("添加失败!");
    },
  });

  return (
    <>
      {contextHolder}
      <ModalForm
        title={"新增活动"}
        trigger={
          <Button type="primary" icon={<PlusOutlined />}>
            新增
          </Button>
        }
        modalProps={{ okButtonProps: { loading } }}
        onFinish={async (value) => {
          await run({ data: value as API.RuleListItem });
          return true;
        }}
      >
        <ProFormText
          label={"活动名"}
          rules={[
            {
              required: true,
            },
          ]}
          name="name"
        />
        <ProFormTextArea name="desc" label="活动描述" />
      </ModalForm>
    </>
  );
};

export default CreateForm;
